<template>
  <div class="app-container btn">
    <el-form
      :model="queryParams"
      label-position="left"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
      size="medium"
    >
      <el-form-item class="item" label="CAS号" prop="casNo">
        <el-input
          v-model="queryParams.casNo"
          placeholder="请输入CAS号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分子量" prop="molecularWeight">
        <el-input
          v-model="queryParams.molecularWeight"
          placeholder="请输入分子量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-input
          v-model="queryParams.status"
          placeholder="请输入状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库时间" prop="addTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.addTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择入库时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['metaRef:Chemsrc:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['metaRef:Chemsrc:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['metaRef:Chemsrc:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['metaRef:Chemsrc:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :header-cell-style="{ 'font-size': '17px' }"
      style="width: 100%; font-size: 17px"
      :data="ChemsrcList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column fixed type="selection" width="55" align="center" />
      <el-table-column fixed label="主键" align="center" prop="id" />
      <el-table-column
        fixed
        width="180"
        label="常用名"
        align="center"
        prop="nameCn"
      />
      <el-table-column
        width="120"
        label="英文名"
        align="center"
        prop="nameEn"
      />
      <el-table-column width="120" label="CAS号" align="center" prop="casNo" />
      <el-table-column
        width="120"
        label="分子量"
        align="center"
        prop="molecularWeight"
      />
      <el-table-column width="150" label="密度" align="center" prop="density" />
      <el-table-column
        width="250"
        label="沸点"
        align="center"
        prop="boilingPoint"
      />
      <el-table-column
        width="150"
        label="熔点"
        align="center"
        prop="meltingPoint"
      />
      <el-table-column
        width="150"
        label="闪点"
        align="center"
        prop="flashPoint"
      />
      <el-table-column
        width="150"
        label="分子式"
        align="center"
        prop="formula"
      />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column
        label="入库时间"
        align="center"
        prop="addTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        width="120"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['metaRef:Chemsrc:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['metaRef:Chemsrc:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      class="pagination_style"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改化源网数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="常用名" prop="nameCn">
          <el-input v-model="form.nameCn" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="英文名" prop="nameEn">
          <el-input v-model="form.nameEn" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="CAS号" prop="casNo">
          <el-input v-model="form.casNo" placeholder="请输入CAS号" />
        </el-form-item>
        <el-form-item label="分子量" prop="molecularWeight">
          <el-input v-model="form.molecularWeight" placeholder="请输入分子量" />
        </el-form-item>
        <el-form-item label="密度" prop="density">
          <el-input v-model="form.density" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="沸点" prop="boilingPoint">
          <el-input v-model="form.boilingPoint" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="熔点" prop="meltingPoint">
          <el-input v-model="form.meltingPoint" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="闪点" prop="flashPoint">
          <el-input v-model="form.flashPoint" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分子式" prop="formula">
          <el-input v-model="form.formula" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="入库时间" prop="addTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.addTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择入库时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listChemsrc,
  getChemsrc,
  delChemsrc,
  addChemsrc,
  updateChemsrc,
  exportChemsrc,
} from "@/api/metaRef/Chemsrc";

export default {
  name: "Chemsrc",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 化源网数据表格数据
      ChemsrcList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nameCn: null,
        nameEn: null,
        casNo: null,
        molecularWeight: null,
        density: null,
        boilingPoint: null,
        meltingPoint: null,
        flashPoint: null,
        formula: null,
        status: null,
        addTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        addTime: [
          { required: true, message: "入库时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询化源网数据列表 */
    getList() {
      this.loading = true;
      listChemsrc(this.queryParams).then((response) => {
        this.ChemsrcList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nameCn: null,
        nameEn: null,
        casNo: null,
        molecularWeight: null,
        density: null,
        boilingPoint: null,
        meltingPoint: null,
        flashPoint: null,
        formula: null,
        status: null,
        addTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加化源网数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getChemsrc(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改化源网数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateChemsrc(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChemsrc(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除化源网数据编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delChemsrc(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有化源网数据数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportChemsrc(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-form-item__label {
  font-size: 17px;
  text-align: center;
}
::v-deep .mb8 .el-button {
  font-size: 16px;
}
::v-deep .el-form .el-button {
  font-size: 16px;
}

::v-deep .el-dialog .el-input__inner {
  width: 300px;
}
</style>
<style lang="scss" scoped>
::v-deep .el-form-item__label {
  font-size: 17px;
  text-align: center;
}
::v-deep .mb8 .el-button {
  font-size: 16px;
}
::v-deep .el-form .el-button {
  font-size: 16px;
}
// ::v-deep .el-form  .el-form-item {
// width: 24%;
// }

::v-deep .el-form .el-form-item {
  .el-form-item__error {
    width: 120px;
  }
}

::v-deep .el-dialog .el-form-item__label {
  width: 30px;
}

//去除input number样式
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
::v-deep input[type="number"] {
  -moz-appearance: textfield !important;
}
::v-deep .el-dialog .el-input__inner {
  width: 300px;
  line-height: 1px !important;
}

.element.style {
  width: 100px;
  min-width: 100px;
}

.app-container {
  background: rgb(254, 254, 254);
}

.pagination_style {
  height: 70px;
  margin-right: 40px;
}

.btn {
  .el-button {
    margin-left: 10px;
    margin-right: 5px;
  }
}
.item {
  margin-left: 20px;
}
</style>
